package com.bhs.sansong.adapter;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansong.R;
import com.bhs.sansong.bean.CompanyInfo;
import com.bhs.sansong.bean.PriceBean;
import com.bhs.sansong.bean.PrivateLines;
import com.bhs.sansong.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.List;

public class ListPrivateLinesAdapter extends BaseQuickAdapter<PrivateLines, BaseViewHolder> {
    private int type;//1选择公司 2找物流 3历史足迹

    public ListPrivateLinesAdapter(int type) {
        super(R.layout.item_collaboration);
        this.type = type;
    }

    public ListPrivateLinesAdapter(int layoutResId, @Nullable List<PrivateLines> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, PrivateLines item) {
        //不要研究这段代码 后端写的好接口
        CompanyInfo data;
        data = item.getLogistics_info();
        if (data == null || data.getCompany_pic() == null) {
            data = item.getLogistics_info().getCompany_info();
        }
        helper.setGone(R.id.ll_browse, type == 3);
        helper.setText(R.id.tv_browse, String.format("%s浏览过", MyUtils.getBefore(item.getVisit_time())));
        Glide.with(mContext).load(data.getCompany_pic()).into((ImageView) helper.getView(R.id.iv_image));
        helper.setText(R.id.tv_company_name, data.getCompany_name());
        String start_city = item.getLine_info().getStart_city().replace("市", "");
        helper.setText(R.id.tv_start_city, start_city);
        String end_city = item.getLine_info().getEnd_city().replace("市", "");
        helper.setText(R.id.tv_end_city, end_city);
        PriceBean priceBean = new Gson().fromJson(item.getLine_info().getGeneral_price_json(), PriceBean.class);
        TextView mTvOffer = helper.getView(R.id.tv_offer);
        helper.setText(R.id.tv_offer, String.format("普运:%s/元吨;%s/立方;%s-%s天",
                priceBean.P1, priceBean.P6, item.getLine_info().getGeneral_day_min(), item.getLine_info().getGeneral_day_max()));
        mTvOffer.setSelected(true);
        helper.setVisible(R.id.rl_line, item.getLine_info().getStart_city() != null);
        helper.setText(R.id.tv_address, data.getAddress());
        helper.setText(R.id.tv_distance, String.format("距您%skm", item.getDistance()));
        helper.setGone(R.id.tv_distance, item.getDistance() > 0);
        Spanned PageView = Html.fromHtml("浏览量:<font color=\"#000000\">" + data.getVisitor_count() + "</font>");
        helper.setText(R.id.tv_page_view, PageView);
        Spanned OrderQuantity = Html.fromHtml("下单量:<font color=\"#000000\">" + data.getOrder_count() + "</font>");
        helper.setText(R.id.tv_order_quantity, OrderQuantity);
        Spanned QuantityCollection = Html.fromHtml("收藏量:<font color=\"#000000\">" + data.getCollect_count() + "</font>");
        helper.setText(R.id.tv_quantity_collection, QuantityCollection);
        TextView mTvAnnouncement = helper.getView(R.id.tv_announcement);
        mTvAnnouncement.setSelected(true);
        helper.setText(R.id.tv_announcement, data.getPublic_notice());
        helper.setGone(R.id.tv_announcement, !TextUtils.isEmpty(data.getPublic_notice()));
        helper.setGone(R.id.ll_mode, type == 1);
        helper.setGone(R.id.ll_operation, type == 2 || type == 3);
        helper.setGone(R.id.ll_offer, item.getLine_info().getStart_city() != null);
        if (type == 1) {
            helper.setText(R.id.tv_number_days, String.format("约%s-%s天送达", item.getLine_info().getGeneral_day_min(), item.getLine_info().getGeneral_day_max()));
            helper.setText(R.id.tv_express, String.format("约%s-%s天送达", item.getLine_info().getFast_day_min(), item.getLine_info().getFast_day_max()));
        } else {
            helper.addOnClickListener(R.id.tv_consent, R.id.ll_contact);
        }
        PrivateLines.LineInfoDTO lineInfo = item.getLine_info();
        helper.setGone(R.id.iv_label, lineInfo.getMember_status().equals("1") || lineInfo.getDeposit_status().equals("1"));
        helper.setGone(R.id.iv_member, lineInfo.getMember_status().equals("1"));
        int promoteType = Integer.parseInt(lineInfo.getPromote_type());
        //会员和推广
        if (lineInfo.getMember_status().equals("1") && promoteType > 0) {
            helper.setImageResource(R.id.iv_label, R.mipmap.ic_promotion_member);
        } else {
            if (lineInfo.getMember_status().equals("1")) {
                helper.setImageResource(R.id.iv_label, R.mipmap.ic_member);
            }
            if (promoteType > 0) {
                helper.setImageResource(R.id.iv_label, R.mipmap.ic_promotion);
            }
        }
        helper.setGone(R.id.ll_quality_special_line, lineInfo.getDeposit_status().equals("1"));
        //品质专线
        if (lineInfo.getDeposit_status().equals("1")) {
            Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "YouSheBiao.ttf");
            helper.setTypeface(typeface, R.id.tv_left, R.id.tv_right);
            if (promoteType > 0) {
                if (promoteType < 5) {
                    helper.setBackgroundRes(R.id.ll_main, R.drawable.bg_gold_card_around);
                    helper.setTextColor(R.id.tv_left, Color.parseColor("#9F581A"));
                } else {
                    helper.setBackgroundRes(R.id.ll_main, R.drawable.bg_silver_card_around);
                    helper.setTextColor(R.id.tv_left, Color.parseColor("#595959"));
                }
            } else {
                helper.setBackgroundRes(R.id.ll_main, R.drawable.bg_quality);
                helper.setTextColor(R.id.tv_left, Color.parseColor("#777777"));
            }
        } else {
            if (promoteType > 0) {
                if (promoteType < 5) {
                    helper.setBackgroundRes(R.id.ll_main, R.drawable.bg_gold_card_fluctuate);
                } else {
                    helper.setBackgroundRes(R.id.ll_main, R.drawable.bg_silver_card_fluctuate);
                }
            }
        }
        helper.setGone(R.id.iv_video, item.getHave_video().equals("1"));

    }
}
